package com.ruoyi.health.report.utils;

import com.ruoyi.common.core.constant.ZkxcRedisConstants;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.health.report.api.IRemoteFileService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * 获取数据时将图片数据保存为url缓存到redis，下次获取数据时从redis中获取图片数据
 * redisKey ==> customerId:reportId:imageKey
 * 为了解决缓存丢失问题，当获取url时 未获取到，则重新将数据缓存 获取url
 *
 * @author ChenYF
 * @date 2025-09-08 10:58
 */
@Component
public class ZkxcImageUtil {
    @Autowired
    RedisService redisService;
    @Autowired
    private IRemoteFileService fileService;

    /**
     * 获取图片url
     * @param customerId
     * @param reportId
     * @param imageKey
     * @param base64Image
     * @return
     */
    public String getImageUrl(Long customerId, Long reportId, String imageKey, String base64Image) {
        String key = getRedisKey(customerId, reportId, imageKey);
        String hKey = encrypt(base64Image);
        String url = checkImage(key, hKey);
        if (ObjectUtils.isNotEmpty(url)) {
            System.out.println(key + "   " + hKey + "   " + url + "  图片存在");
            return url;
        }
        return saveImageUrl(key, hKey, base64Image);
    }

    /**
     * 设备推送数据时将需要保存为url的图片进行存储
     *
     * @param customerId
     * @param reportId
     * @param imageKey
     * @param base64Image
     */
    public String saveImageUrl(Long customerId, Long reportId, String imageKey, String base64Image) {
        String key = getRedisKey(customerId, reportId, imageKey);
        String hKey = encrypt(base64Image);

        // 判断地址是否存在
        Object cacheMapValue = redisService.getCacheMapValue(key, hKey);
        if (ObjectUtils.isNotEmpty(cacheMapValue)) {
            return cacheMapValue.toString();
        }

        MultipartFile file = Base64ToMultipartFile.base64Convert(base64Image);
        String url = fileService.upload(file, null).getData().getUrl();
        // 保存url 到 redis
        redisService.setCacheMapValue(key, hKey, url);
        return url;
    }

    /**
     * 保存图片
     *
     * @param key
     * @param hKey
     * @param base64Image
     * @return
     */
    public String saveImageUrl(String key, String hKey, String base64Image) {
        MultipartFile file = Base64ToMultipartFile.base64Convert(base64Image);
        String url = fileService.upload(file, null).getData().getUrl();
        System.out.println("注解保存图片 file模块返回地址：" + url);
        // 保存url 到 redis
        redisService.setCacheMapValue(key, hKey, url);
        return url;
    }

    public static String encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger no = new BigInteger(1, messageDigest);
            String hashText = no.toString(16);

            return hashText.substring(0, 16); // 只取前16位
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取redisKey
     *
     * @param customerId
     * @param reportId
     * @param imageKey
     * @return
     */
    public String getRedisKey(Long customerId, Long reportId, String imageKey) {
        return String.format(ZkxcRedisConstants.ImageKey, customerId, reportId, imageKey);
    }

    public String checkImage(String key, String hKey) {
        // 判断地址是否存在
        Object cacheMapValue = redisService.getCacheMapValue(key, hKey);
        if (ObjectUtils.isNotEmpty(cacheMapValue)) {
            return cacheMapValue.toString();
        }
        return null;
    }
}
